#!/bin/bash

local=$1
# 定义要安装的软件包列表  
packages=(  
    dhcp  
    vsftpd  
    xinetd  
    samba  
    bind  
    bind-utils  
    httpd  
    tftp-server  
    mariadb-server  
    php  
    php-mysql
    unzip
    wget  
)  
# 遍历软件包列表并安装未安装的软件包  
for pkg in "${packages[@]}"; do  
    if ! yum list installed | grep -q "^${pkg}\."; then  
        echo "软件包 $pkg 未安装，正在安装..."  
        yum -y install "$pkg"  
        if [ $? -ne 0 ]; then  
            echo "安装软件包 $pkg 失败."  
            exit 1  
        fi  
    else  
        echo "软件包 $pkg 已安装."  
    fi  
done  
# 脚本结束，所有软件包已检查并安装（或确认已安装）  
echo "所有必要的软件包已安装或已确认存在."
# 创建TFTP引导目录
#mkdir -p /var/lib/tftpboot
# DHCP服务器的配置
cat > /etc/dhcp/dhcpd.conf <<EOF
subnet 10.10.10.0 netmask 255.255.255.0 {    
range 10.10.10.100 10.10.10.200;    
option routers 10.10.10.1;    
option domain-name-servers $local;   
option routers 10.10.10.254; 
next-server $local;    
filename "pxelinux.0";    
default-lease-time 600;    
max-lease-time 7200;
}
EOF
# 启动并设置DHCP
systemctl start dhcpd
read -p " 按任意键继续吧！"
# TFTP服务的配置
sed -i 's/disable\t\t\t= yes/disable\t\t\t= no/g' /etc/xinetd.d/tftp
# 重启xinetd服务
systemctl restart xinetd
read -p " 按任意键继续吧！"
#下载引导程序
tftproot=/var/lib/tftpboot
wget http://stu.jxit.net.cn:88/jxfiles/liuyan.tar.gz -O $tftproot/syslinux.tar.gz
tar -zxf $tftproot/syslinux.tar.gz -C $tftproot
mv $tftproot/syslinux604files/* $tftproot
#创建启动菜单
mkdir -p $tftproot/pxelinux.cfg
cat > $tftproot/pxelinux.cfg/default <<EOF
DEFAULT menu.c32
LABEL win8pe
KERNEL memdisk
APPEND initrd=ftp://$local/pub/win8pe.iso iso raw
EOF
#下载镜像到ftp匿名根目录
wget http://stu.jxit.net.cn:88/iso/Win8PEx64.iso -O /var/ftp/pub/win8pe.iso
systemctl restart vsftpd
# Samba的配置
mkdir -p /var/lib/samba/game
wget http://stu.jxit.net.cn:88/jxfiles/EIGHT.zip -O /var/lib/samba/game/EIGHT.zip
unzip EIGHT.zip -d /var/lib/samba/game
chmod +x EIGHT.exe
cat > /etc/samba/smb.conf <<EOF
[game]        
path = /var/lib/samba/game        
guest ok = Yes        
EOF
systemctl restart smb
# DNS服务的配置
sed -i 's/127.0.0.1/any/g' /etc/named.conf
sed -i 's/localhost/any/g' /etc/named.conf
# 创建DNS区域文件目录
mkdir -p /var/named
cat >> /etc/named.rfc1912.zones <<EOF
zone "jxit.net.cn" IN {    
type master;    
file "named.jxit.net.cn";    
allow-update { none; };
};
EOF
# 添加DNS记录
echo "dl      A       192.168.1.55" >> /var/named/named.jxit.net.cn
echo "discuz	A	10.10.10.13" >> /var/named/named.jxit.net.cn
# 设置文件权限
chown named:named /var/named/named.jxit.net.cn
# 启动DNS服务
systemctl start named
# LAMP的配置（Apache, MySQL, PHP）
#初始化管理员密码
systemctl start mariadb
mysqladmin -u root -p password '123456'
systemctl start httpd
#部署网站 下载并配置Discuz论坛
wget http://stu.jxit.net.cn:88/jxfiles/Discuz_7.2_FULL_SC_UTF8.zip -O /tmp/a.zip
# 检查并创建网站目录
website_dir="/var/www/html"
upload_dir="$website_dir/upload"
mkdir -p "$upload_dir/images/attachicons"
# 解压缩Discuz论坛文件
#unzip /tmp/a.zip -d "$website_dir"
#mv "$website_dir/upload"/* "$upload_dir"
# 解压缩Discuz论坛文件
unzip /tmp/a.zip -d "$website_dir" --no-overwrite
# 检查并处理readme目录中的文件
readme_dir="$website_dir/readme"
for file in $(ls "$readme_dir"); do
    if [ -f "$readme_dir/$file" ]; then
# 如果文件已存在，且不是我们要保留的文件，则备份或删除
        if ! echo "changelog.txt convert.txt license.txt readme.txt upgrade.txt" | grep -q "$file"; then            
mv "$readme_dir/$file" "$readme_dir/backup_$file"        
fi    
fi
done
# 同样的方法处理upload目录中的文件
upload_dir="$website_dir/upload"
for file in $(ls "$upload_dir"); do
    if [ -f "$upload_dir/$file" ]; then
# 如果文件已存在，且不是我们要保留的文件，则备份或删除
        if ! echo "admincp.php admin/addons.inc.php admin/admingroups.inc.php admin/adv.inc.php admin/announce.inc.php" | grep -q "$file"; then            
mv "$upload_dir/$file" "$upload_dir/backup_$file"        
fi    
fi
done
# 检查并处理attachicons目录中的文件
attachicons_dir="$upload_dir/images/attachicons"
for file in $(ls "$attachicons_dir"); do
    if [ -f "$attachicons_dir/$file" ]; then
# 如果文件已存在，且不是我们要保留的文件，则备份或删除
        if ! 
echo "binary.gif common.gif common_new.gif flash.gif html.gif image.gif image_s.gif" | grep -q "$file"; then            
mv "$attachicons_dir/$file" "$attachicons_dir/backup_$file"        
fi    
fi
done
# 修改PHP配置
sed -i 's/short_open_tag= Off/short_open_tag= On/g' /etc/php.ini
# 重启Apache服务
systemctl restart httpd
# 设置目录权限
chown -R apache:apache "$website_dir"


#wget http://stu.jxit.net.cn:88/jxfiles/Discuz_7.2_FULL_SC_UTF8.zip -O /tmp/a.zip
#unzip /tmp/a.zip -d /var/www/html
#mv /var/www/html/upload/* /var/www/html/
# 修改PHP配置
sed -i 's/short_open_tag= Off/short_open_tag= On/g' /etc/php.ini
systemctl restart httpd
# 设置目录权限
chown -R apache:apache .
# 禁用防火墙
systemctl stop firewalld
# 设置SELinux为宽容模式
setenforce 0
